package me.zhengjie.modules.quartz.task;

import com.intelligt.modbus.jlibmodbus.Modbus;
import com.intelligt.modbus.jlibmodbus.exception.ModbusIOException;
import com.intelligt.modbus.jlibmodbus.exception.ModbusNumberException;
import com.intelligt.modbus.jlibmodbus.exception.ModbusProtocolException;
import com.intelligt.modbus.jlibmodbus.master.ModbusMaster;
import com.intelligt.modbus.jlibmodbus.master.ModbusMasterFactory;
import com.intelligt.modbus.jlibmodbus.tcp.TcpParameters;
import me.zhengjie.config.Constant;

import java.net.InetAddress;
import java.util.concurrent.ExecutionException;

public class JLibModbus {

    public static Integer getValue(String IP, int port, int address, int quantity)
            throws InterruptedException, ExecutionException, RuntimeException, Exception,
            ModbusProtocolException, ModbusNumberException, ModbusIOException {
        // 设置主机TCP参数
        TcpParameters tcpParameters = new TcpParameters();

        // 设置TCP的ip地址
        InetAddress adress = InetAddress.getByName(IP);

        // TCP参数设置ip地址
        // tcpParameters.setHost(InetAddress.getLocalHost());
        tcpParameters.setHost(adress);

        // TCP设置长连接
        tcpParameters.setKeepAlive(false);
        // TCP设置端口，这里设置是默认端口502
        tcpParameters.setPort(port);

        // 创建一个主机
        ModbusMaster master = ModbusMasterFactory.createModbusMasterTCP(tcpParameters);
        Modbus.setAutoIncrementTransactionId(true);

        int slaveId = 1;//从机地址
//            int offset = 1;//寄存器读取开始地址
//            int quantity = 10;//读取的寄存器数量


        if (!master.isConnected()) {
            master.connect();// 开启连接
        }
        // 读取对应从机的数据，readInputRegisters读取的写寄存器，功能码04
        int[] registerValues = master.readHoldingRegisters(slaveId, address, quantity);
//                boolean[] registerValues = master.readCoils(slaveId,offset,quantity) ;
        // 控制台输出
        for (int value : registerValues) {
            System.out.println("Address: " + address++ + ", Value: " + value);
        }
        if (registerValues.length > 0) {
            return registerValues[0];
        }
        Constant.lock = false;
        return null;
    }

    public static void setValue(String IP, int port, int address, int value) throws InterruptedException, ExecutionException, RuntimeException, Exception,
            ModbusProtocolException, ModbusNumberException, ModbusIOException {
        // 设置主机TCP参数
        TcpParameters tcpParameters = new TcpParameters();

        // 设置TCP的ip地址
        InetAddress adress = InetAddress.getByName(IP);

        // TCP参数设置ip地址
        // tcpParameters.setHost(InetAddress.getLocalHost());
        tcpParameters.setHost(adress);
        // TCP设置长连接
        tcpParameters.setKeepAlive(false);
        // TCP设置端口，这里设置是默认端口502
        tcpParameters.setPort(port);
        // 创建一个主机
        ModbusMaster master = ModbusMasterFactory.createModbusMasterTCP(tcpParameters);
        Modbus.setAutoIncrementTransactionId(true);
        int slaveId = 1;//从机地址
//            int offset = 1;//寄存器读取开始地址
//            int quantity = 10;//读取的寄存器数量
        if (!master.isConnected()) {
            master.connect();// 开启连接
        }
        // 写入对应从机的数据，writeInputRegisters读取的写寄存器，功能码04
        master.writeSingleRegister(slaveId, address, value);
        Constant.lock = false;
    }

}
